﻿using DLL;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.PICCNursingConsent
{
    public partial class ConsentPrints : Form
    {
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        string Id;
        public ConsentPrints(Tb_User loginUser,PatientInfo patientInfo,string id)
        {
            InitializeComponent();
            this.PatientInfo = patientInfo;
            this.Id = id;
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\PICC同意书2.grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            axGRPrintViewer1.Report = Report;
        }

        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecord(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.WrittenConsentNursing);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                if (!string.IsNullOrEmpty(this.Id))
                    list = list.Where(p => p.Id == Convert.ToInt32(this.Id)).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record = list[i];
                    WrittenConsent ps = XmlUtil.XMLToObject<WrittenConsent>(record.NursingFormData, Encoding.UTF8);
                    dt.Rows.Add(
                        PatientInfo.PATIENT_NAME,
                        PatientInfo.SEX,
                        PatientInfo.AGE,
                        PatientInfo.NURSE_CELL_NAME,
                        PatientInfo.BED_NO,
                        PatientInfo.PATIENT_ID,
                        PatientInfo.IN_DATE,
                        PatientInfo.IN_DEPARTMENT_DATE,
                        PatientInfo.DIAGNOSE,
                        PatientInfo.ALLERGY,
                        ps.PICCShiYingZheng.Split('#')[0],
                        ps.PICCShiYingZheng.Split('#')[1],
                        ps.PICCShiYingZheng.Split('#')[2],
                        ps.PICCShiYingZheng.Split('#')[3],
                        ps.PICCShiYingZheng.Split('#')[4],
                        ps.ZhiGuanGuoCheng.Split('#')[0],
                        ps.ZhiGuanGuoCheng.Split('#')[1],
                        ps.ZhiGuanGuoCheng.Split('#')[2],
                        ps.ZhiGuanGuoCheng.Split('#')[3],
                        ps.ShiYongGuoCheng.Split('#')[0],
                        ps.ShiYongGuoCheng.Split('#')[1],
                        ps.ShiYongGuoCheng.Split('#')[2],
                        ps.ShiYongGuoCheng.Split('#')[3],
                        ps.ShiYongGuoCheng.Split('#')[4],
                        ps.ShiYongGuoCheng.Split('#')[5],
                        ps.ShiYongGuoCheng.Split('#')[6],
                        ps.ShiYongGuoCheng.Split('#')[7],
                        ps.HuanErJianHuRenYiJian,
                        ps.QianMing,
                        Convert.ToDateTime(ps.QingMingShiJian).ToString("yyyy"),
                        Convert.ToDateTime(ps.QingMingShiJian).ToString("MM"),
                        Convert.ToDateTime(ps.QingMingShiJian).ToString("dd"),
                        Convert.ToDateTime(ps.QingMingShiJian).ToString("HH"),
                        Convert.ToDateTime(ps.QingMingShiJian).ToString("mm"),
                        ps.HuanZheShouQuanWeiTuoRenYiJian,
                        ps.YuHuanGuanXi,
                        ps.WeiTuoRenQianMing,
                        ps.WeiTuoRenLianXiFangShi,
                        Convert.ToDateTime(ps.QingMingShiJian2).ToString("yyyy"),
                        Convert.ToDateTime(ps.QingMingShiJian2).ToString("MM"),
                        Convert.ToDateTime(ps.QingMingShiJian2).ToString("dd"),
                        Convert.ToDateTime(ps.QingMingShiJian2).ToString("HH"),
                        Convert.ToDateTime(ps.QingMingShiJian2).ToString("mm"),
                        ps.HuoDeShouQianDeYiWuRenYuanQiaMming,
                        Convert.ToDateTime(ps.QingMingShiJian3).ToString("yyyy"),
                        Convert.ToDateTime(ps.QingMingShiJian3).ToString("MM"),
                        Convert.ToDateTime(ps.QingMingShiJian3).ToString("dd"),
                        Convert.ToDateTime(ps.QingMingShiJian3).ToString("HH"),
                        Convert.ToDateTime(ps.QingMingShiJian3).ToString("mm"),
                        ps.ZhuZhiYiShiQianMing,
                        Convert.ToDateTime(ps.QingMingShiJian4).ToString("yyyy"),
                        Convert.ToDateTime(ps.QingMingShiJian4).ToString("MM"),
                        Convert.ToDateTime(ps.QingMingShiJian4).ToString("dd"),
                        Convert.ToDateTime(ps.QingMingShiJian4).ToString("HH"),
                        Convert.ToDateTime(ps.QingMingShiJian4).ToString("mm"),
                        ps.PICCShiYingZhengWenBenKuang
                        );

                }
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }

        private void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("PatientName", typeof(string));
            dt.Columns.Add("sex", typeof(string));
            dt.Columns.Add("age", typeof(string));
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PatientId", typeof(string));
            dt.Columns.Add("IN_DATE", typeof(string));
            dt.Columns.Add("IN_DEPARTMENT_DATE", typeof(string));
            dt.Columns.Add("DIAGNOSE", typeof(string));
            dt.Columns.Add("ALLERGY", typeof(string));
            dt.Columns.Add("PICCshiyingzheng1", typeof(string));
            dt.Columns.Add("PICCshiyingzheng2", typeof(string));
            dt.Columns.Add("PICCshiyingzheng3", typeof(string));
            dt.Columns.Add("PICCshiyingzheng4", typeof(string));
            dt.Columns.Add("PICCshiyingzheng5", typeof(string));
            dt.Columns.Add("zhiguanguocheng1", typeof(string));
            dt.Columns.Add("zhiguanguocheng2", typeof(string));
            dt.Columns.Add("zhiguanguocheng3", typeof(string));
            dt.Columns.Add("zhiguanguocheng4", typeof(string));
            dt.Columns.Add("shiyongguocheng1", typeof(string));
            dt.Columns.Add("shiyongguocheng2", typeof(string));
            dt.Columns.Add("shiyongguocheng3", typeof(string));
            dt.Columns.Add("shiyongguocheng4", typeof(string));
            dt.Columns.Add("shiyongguocheng5", typeof(string));
            dt.Columns.Add("shiyongguocheng6", typeof(string));
            dt.Columns.Add("shiyongguocheng7", typeof(string));
            dt.Columns.Add("shiyongguocheng8", typeof(string));
            dt.Columns.Add("huanerjianhurenyijian", typeof(string));
            dt.Columns.Add("qianming", typeof(string));
            dt.Columns.Add("qianmingshijian1", typeof(string));
            dt.Columns.Add("qianmingshijian2", typeof(string));
            dt.Columns.Add("qianmingshijian3", typeof(string));
            dt.Columns.Add("qianmingshijian4", typeof(string));
            dt.Columns.Add("qianmingshijian5", typeof(string));
            dt.Columns.Add("huanzheshouquanweituorenyijian", typeof(string));
            dt.Columns.Add("yuhuanzheguanxi", typeof(string));
            dt.Columns.Add("weituorenqianming", typeof(string));
            dt.Columns.Add("weituorenlianxifangshi", typeof(string));
            dt.Columns.Add("qianmingshijian21", typeof(string));
            dt.Columns.Add("qianmingshijian22", typeof(string));
            dt.Columns.Add("qianmingshijian23", typeof(string));
            dt.Columns.Add("qianmingshijian24", typeof(string));
            dt.Columns.Add("qianmingshijian25", typeof(string));
            dt.Columns.Add("huodeshouquandeyiwurenyuanqianming", typeof(string));
            dt.Columns.Add("qianmingshijian31", typeof(string));
            dt.Columns.Add("qianmingshijian32", typeof(string));
            dt.Columns.Add("qianmingshijian33", typeof(string));
            dt.Columns.Add("qianmingshijian34", typeof(string));
            dt.Columns.Add("qianmingshijian35", typeof(string));
            dt.Columns.Add("zhuzhiyishiqianming", typeof(string));
            dt.Columns.Add("qianmingshijian41", typeof(string));
            dt.Columns.Add("qianmingshijian42", typeof(string));
            dt.Columns.Add("qianmingshijian43", typeof(string));
            dt.Columns.Add("qianmingshijian44", typeof(string));
            dt.Columns.Add("qianmingshijian45", typeof(string));
            dt.Columns.Add("PICCshiyingzheng6", typeof(string));
        }
        /// <summary>
        /// 检查数据项目
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        private bool ChkStr(string str, int len)
        {
            if (string.IsNullOrEmpty(str)) return false;
            if (str.IndexOf("#") < 0) return false;
            if (str.Split('#').Length < len) return false;
            return true;
        }
        private void BilirubinPrints_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
    }
}
